//
//  HNSH_ShopDetailViewController.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/3/2.
//

#define KTopViewHeight      48

#import "HNSH_ShopDetailViewController.h"

//view
#import "HNSH_HomeGoodsCell.h"
#import "HNSH_ShopDetailTopView.h"

//vc
#import "HNSH_GoodsDetailViewController.h"
#import "HNSH_SearchGoodsViewController.h"

@interface HNSH_ShopDetailViewController ()

/* listAry */
@property (nonatomic, strong) NSMutableArray *listAry;

/* topView */
@property (nonatomic, strong) HNSH_ShopDetailTopView *topView;

@end

static NSString *HNSH_HomeGoodsCellID = @"HNSH_HomeGoodsCellID";

@implementation HNSH_ShopDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self showNavTitle:self.shopName showLeftItem:YES];
    
    [self configUI];
}

- (void)configUI {
    [self.view addSubview:self.topView];
    
    [self configCollectionView];
}

- (void)configCollectionView {
    [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(kNavBarAndStatusBarHeight+KTopViewHeight, 0, 0, 0));
    }];
    
    self.collectionView.backgroundColor = [UIColor colorWithHexString:@"#F9F9F9"];
    
    [self.collectionView registerClass:[HNSH_HomeGoodsCell class] forCellWithReuseIdentifier:HNSH_HomeGoodsCellID];
    
    //添加刷新
    [self setupRefresh:self.collectionView option:ST_RefreshDefault];
    //添加空页面
    [self setupEmpty:self.collectionView image:[UIImage imageNamed:@"empty_list_data"] title:@""];
}

#pragma mark networking
- (void)refreshData:(NSInteger)page {
    [HNSH_ShopNetServer requestGoodsListWithShop_id:self.shopId mcate_id:@"" scate_id:@"" keyword:@"" sort_type:@"" sort_rule:@"" type:@"" page_num:page success:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            if (page == 1) {
                [self.listAry removeAllObjects];
            }
            NSArray *ary = [NSArray yy_modelArrayWithClass:[HNSH_GoodsListModel class] json:responseObject.resultset[@"list"]];
            [self.listAry addObjectsFromArray:ary];
            [self.collectionView reloadData];
            [self endRefresh:ary.count >= 10];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

#pragma mark lazy
- (HNSH_ShopDetailTopView *)topView {
    if (!_topView) {
        _topView = [[HNSH_ShopDetailTopView alloc]initWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight, kScreenWidth, KTopViewHeight)];
        WeakSelf(weakSelf);
        _topView.searchBlock = ^{
            HNSH_SearchGoodsViewController *vc = [[HNSH_SearchGoodsViewController alloc]init];
            vc.shop_id = self.shopId;
            [weakSelf.navigationController pushViewController:vc animated:YES];
        };
    }
    return _topView;
}

- (NSMutableArray *)listAry {
    if (!_listAry) {
        _listAry = [NSMutableArray arrayWithCapacity:0];
    }
    return _listAry;
}

#pragma mark UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.listAry.count;
}
//设置海报图片
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HNSH_HomeGoodsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:HNSH_HomeGoodsCellID forIndexPath:indexPath];
    cell.model = self.listAry[indexPath.row];
    return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    UICollectionReusableView *reusableView = [collectionView supplementaryViewForElementKind:NSStringFromClass(UICollectionReusableView.class) atIndexPath:indexPath];
    return reusableView;
}
#pragma mark UICollectionViewDelegateFlowLayout
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 12, 8, 12);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat width = (kScreenWidth-32)/2;
    return CGSizeMake(width, width+91);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 8;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 8;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeZero;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    return CGSizeZero;
}
#pragma mark UICollectionViewDataSource
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    HNSH_GoodsListModel *model = self.listAry[indexPath.row];
    HNSH_GoodsDetailViewController *vc = [[HNSH_GoodsDetailViewController alloc]init];
    vc.goodsId = model.uid;
    [self.navigationController pushViewController:vc animated:YES];
}


@end
